﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using ZLCMS.Web.SystemManage.Basics.ProductSKU;
using DotNet.Common.NHibernateUtil;
using NHibernate;
using NHibernate.Criterion;
using Newtonsoft.Json;
using ZLCMS.Business.Util;
using ZLCMS.Business.Common;
using ZLCMS.Business.Shop.Data;

namespace ZLCMS.Web.Product
{
    public partial class ProductDetail : System.Web.UI.Page
    {
        public string firstPhoto;
        public ShopProductInfo _productInfo;
        public string _msg_title = "";
        public List<CustomData> _productGroupList;
        public ShopSkuProduct _skuProduct;
        public IList<ShopSkuProduct> _skuProductList;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["openid"] = Request["openid"];
                //   ViewState["account"] = Request["account"];
            }


            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                try
                {
                    AdsKits.BindAdsProductByPositionCode(rpt_slide1, session, "PRODUCT_TOP_SLIDE", -1);
                //    AdsKits.BindAdsProductByPositionCode(rpt_slide2, session, "PRODUCT_BOTTOM_SLIDE", -1);

                    _productInfo = session.CreateCriteria<ShopProductInfo>().Add(Expression.Eq("OwnerAccount", "admin")).Add(Expression.Eq("ProductNo", Request["no"])).UniqueResult<ShopProductInfo>();
                    if (_productInfo != null)
                    { 
                       _skuProductList = session.CreateCriteria<ShopSkuProduct>().Add(Expression.Eq("ProductNo", _productInfo.ProductNo)).List<ShopSkuProduct>();


                    _productGroupList = JsonConvert.DeserializeObject<List<CustomData>>(_productInfo.CustomField);
                 

                    //获取所有存在的SKUItem
                    List<SKUGroup> skuGroupList = new List<SKUGroup>();
                    foreach (ShopSkuProduct p in _skuProductList)
                    {
                        SKUGroup[] list = JsonConvert.DeserializeObject<SKUGroup[]>(p.ParamCode);
                        foreach (SKUGroup g in list)
                        {
                            bool Ismatch = false;
                            foreach (SKUGroup availableGroup in skuGroupList)
                            {
                                if (availableGroup.GroupId == g.GroupId && availableGroup.ItemId == g.ItemId)
                                {
                                    Ismatch = true;
                                }
                            }
                            if (Ismatch == true)
                            {
                                skuGroupList.Add(g);
                            }
                        }
                    } 
                    }
                 

                    if (_productInfo == null)
                    {
                        _msg_title = "对不起,查询不到该产品信息.";
                    }
                    else
                    {
                        List<SimpleFileInfo> filelist = FileKit.FileList(_productInfo.ProductImg);
                        rpt_product_photo.DataSource = filelist;

                        rpt_product_photo.DataBind();

                        firstPhoto = FileKit.FileList(_productInfo.ProductImg)[0].FilePath;
                    }
                }
                catch (Exception ex)
                {
                    _msg_title = "查询出错!" + ex.Message;
                }
            }
        }
    }
}